home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htsbase.h < prev    next >
C/C++ Source or Header  |  2006-08-15  |  10KB  |  382 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Basic definitions                                      */
  34. /*       Used in .c files for basic (malloc() ..) definitions   */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #ifndef HTS_BASICH
  39. #define HTS_BASICH
  40.  
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44.  
  45. #include "htsglobal.h"
  46. #include "htsstrings.h"
  47.  
  48. #include <string.h>
  49. #include <time.h>
  50.  
  51. #ifdef HAVE_UNISTD_H
  52. #include <unistd.h>
  53. #endif
  54. #if ( defined(_WIN32) ||defined(HAVE_SYS_TYPES_H) )
  55. #include <sys/types.h>
  56. #endif
  57. #ifdef HAVE_SYS_STAT_H
  58. #include <sys/stat.h>
  59. #endif
  60. #ifdef HAVE_DLFCN_H
  61. #include <dlfcn.h>
  62. #endif
  63.  
  64. #include <errno.h>
  65.  
  66. #ifdef _WIN32
  67. #else
  68. #include <fcntl.h>
  69. #endif
  70. #include <assert.h>
  71.  
  72. /* GCC extension */
  73. #ifndef HTS_UNUSED
  74. #ifdef __GNUC__
  75. #define HTS_UNUSED __attribute__ ((unused))
  76. #define HTS_STATIC static __attribute__ ((unused))
  77. #else
  78. #define HTS_UNUSED
  79. #define HTS_STATIC static
  80. #endif
  81. #endif
  82.  
  83. #undef min
  84. #undef max
  85. #define min(a,b) ((a)>(b)?(b):(a))
  86. #define max(a,b) ((a)>(b)?(a):(b))
  87.  
  88. #ifndef _WIN32
  89. #undef Sleep
  90. #define min(a,b) ((a)>(b)?(b):(a))
  91. #define max(a,b) ((a)>(b)?(a):(b))
  92. #define Sleep(a) { if (((a)*1000)%1000000) usleep(((a)*1000)%1000000); if (((a)*1000)/1000000) sleep(((a)*1000)/1000000); }
  93. #endif
  94.  
  95. // teste ΘgalitΘ de 2 chars, case insensitive
  96. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  97. #define streql(a,b) (hichar(a)==hichar(b))
  98.  
  99. // caractΦre maj
  100. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  101.  
  102.  
  103. /* Library internal definictions */
  104. #ifdef HTS_INTERNAL_BYTECODE
  105.  
  106.  
  107. // functions
  108. #ifdef _WIN32
  109. #define DynamicGet(handle, sym) GetProcAddress(handle, sym)
  110. #else
  111. #define DynamicGet(handle, sym) dlsym(handle, sym)
  112. #endif
  113.  
  114. // emergency log
  115. typedef void (*t_abortLog)(char* msg, char* file, int line);
  116. extern HTSEXT_API t_abortLog abortLog__;
  117. #define abortLog(a) abortLog__(a, __FILE__, __LINE__)
  118. #define _ ,
  119. #ifndef _WIN32_WCE
  120. #define abortLogFmt(a) do { \
  121.   FILE* fp = fopen("CRASH.TXT", "wb"); \
  122.   if (!fp) fp = fopen("/tmp/CRASH.TXT", "wb"); \
  123.   if (!fp) fp = fopen("C:\\CRASH.TXT", "wb"); \
  124.   if (!fp) fp = fopen("\\Temp\\CRASH.TXT", "wb"); \
  125.   if (!fp) fp = fopen("\\CRASH.TXT", "wb"); \
  126.   if (!fp) fp = fopen("CRASH.TXT", "wb"); \
  127.   if (fp) { \
  128.     fprintf(fp, "HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\n", __LINE__); \
  129.     fprintf(fp, "Reason:\r\n"); \
  130.     fprintf(fp, a); \
  131.     fprintf(fp, "\r\n"); \
  132.     fflush(fp); \
  133.     fclose(fp); \
  134.   } \
  135. } while(0)
  136. #else
  137. #define abortLogFmt(a) do { \
  138.   XCEShowMessageA("HTTrack " HTTRACK_VERSIONID " closed at '" __FILE__ "', line %d\r\nReason:\r\n%s\r\n", __LINE__, a); \
  139. } while(0)
  140. #endif
  141.  
  142. #define assertf(exp) do { \
  143.   if (! ( exp ) ) { \
  144.     abortLog("assert failed: " #exp); \
  145.     if (htsCallbackErr != NULL) { \
  146.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  147.     } \
  148.     assert(exp); \
  149.     abort(); \
  150.   } \
  151. } while(0)
  152. /* non-fatal assert */
  153. #define assertnf(exp) do { \
  154.   if (! ( exp ) ) { \
  155.     abortLog("assert failed: " #exp); \
  156.     if (htsCallbackErr != NULL) { \
  157.       htsCallbackErr("assert failed: " #exp, __FILE__ , __LINE__ ); \
  158.     } \
  159.   } \
  160. } while(0)
  161.  
  162. /* logging */
  163. typedef enum {
  164.     LOG_DEBUG,
  165.     LOG_INFO,
  166.     LOG_WARNING,
  167.     LOG_ERROR,
  168.     LOG_PANIC
  169. } HTS_LogType;
  170. #define HTS_LOG(OPT,TYPE) do { \
  171.   int last_errno = errno; \
  172.     switch(TYPE) { \
  173.     case LOG_DEBUG: \
  174.         fspc(OPT,(OPT)->log, "debug"); \
  175.         break; \
  176.     case LOG_INFO: \
  177.         fspc(OPT,(OPT)->log, "info"); \
  178.         break; \
  179.     case LOG_WARNING: \
  180.         fspc(OPT,(OPT)->log, "warning"); \
  181.         break; \
  182.     case LOG_ERROR: \
  183.         fspc(OPT,(OPT)->log, "error"); \
  184.         break; \
  185.     case LOG_PANIC: \
  186.         fspc(OPT,(OPT)->log, "panic"); \
  187.         break; \
  188.     } \
  189.   errno = last_errno; \
  190. } while(0)
  191.  
  192. /* regular malloc's() */
  193. #ifndef HTS_TRACE_MALLOC
  194. #define malloct(A)          malloc(A)
  195. #define calloct(A,B)        calloc((A), (B))
  196. #define freet(A)            do { assertnf((A) != NULL); if ((A) != NULL) { free(A); (A) = NULL; } } while(0)
  197. #define strdupt(A)          strdup(A)
  198. #define realloct(A,B)       ( ((A) != NULL) ? realloc((A), (B)) : malloc(B) )
  199. #define memcpybuff(A, B, N) memcpy((A), (B), (N))
  200. #else
  201. /* debug version */
  202. #define malloct(A)    hts_malloc(A)
  203. #define calloct(A,B)  hts_calloc(A,B)
  204. #define strdupt(A)    hts_strdup(A)
  205. #define freet(A)      do { hts_free(A); (A) = NULL; } while(0)
  206. #define realloct(A,B) hts_realloc(A,B)
  207. void  hts_freeall();
  208. void* hts_malloc    (size_t);
  209. void* hts_calloc(size_t,size_t);
  210. char* hts_strdup(char*);
  211. void* hts_xmalloc(size_t,size_t);
  212. void  hts_free      (void*);
  213. void* hts_realloc   (void*,size_t);
  214. mlink* hts_find(char* adr);
  215. /* protected memcpy */
  216. #define memcpybuff(A, B, N) do { \
  217.   mlink* lnk = hts_find((void*)(A)); \
  218.   if (lnk != NULL) { \
  219.     assertf(lnk != NULL); \
  220.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) - sizeof(htsboundary) ) ) == htsboundary ); \
  221.     assertf( * ( (t_htsboundary*) ( ((char*) lnk->adr) + lnk->len ) ) == htsboundary ); \
  222.     assertf( ( ((char*)(A)) + (N)) < (char*) (lnk->adr + lnk->len) ); \
  223.   } \
  224.   memcpy(A, B, N); \
  225. } while(0)
  226.  
  227. #endif
  228.  
  229. typedef void (* htsErrorCallback)(char* msg, char* file, int line);
  230. extern HTSEXT_API htsErrorCallback htsCallbackErr;
  231. extern HTSEXT_API int htsMemoryFastXfr;
  232.  
  233. /*
  234. */
  235.  
  236. #define stringdup()
  237.  
  238. #ifdef STRDEBUG
  239.  
  240. /* protected strcat, strncat and strcpy - definitely useful */
  241. #define strcatbuff(A, B) do { \
  242.   assertf( (A) != NULL ); \
  243.   if ( ! (B) ) { assertf( 0 ); } \
  244.   if (htsMemoryFastXfr) { \
  245.     if (sizeof(A) != sizeof(char*)) { \
  246.       (A)[sizeof(A) - 1] = '\0'; \
  247.     } \
  248.     strcat(A, B); \
  249.     if (sizeof(A) != sizeof(char*)) { \
  250.       assertf((A)[sizeof(A) - 1] == '\0'); \
  251.     } \
  252.   } else { \
  253.     unsigned int sz = (unsigned int) strlen(A); \
  254.     unsigned int szf = (unsigned int) strlen(B); \
  255.     if (sizeof(A) != sizeof(char*)) { \
  256.       assertf(sz + szf + 1 < sizeof(A)); \
  257.       if (szf > 0) { \
  258.         if (sz + szf + 1 < sizeof(A)) { \
  259.           memcpy((A) + sz, (B), szf + 1); \
  260.         } \
  261.       } \
  262.     } else if (szf > 0) { \
  263.       memcpybuff((A) + sz, (B), szf + 1); \
  264.     } \
  265.   } \
  266. } while(0)
  267. #define strncatbuff(A, B, N) do { \
  268.   assertf( (A) != NULL ); \
  269.   if ( ! (B) ) { assertf( 0 ); } \
  270.   if (htsMemoryFastXfr) { \
  271.     if (sizeof(A) != sizeof(char*)) { \
  272.       (A)[sizeof(A) - 1] = '\0'; \
  273.     } \
  274.     strncat(A, B, N); \
  275.     if (sizeof(A) != sizeof(char*)) { \
  276.       assertf((A)[sizeof(A) - 1] == '\0'); \
  277.     } \
  278.   } else { \
  279.     unsigned int sz = (unsigned int) strlen(A); \
  280.     unsigned int szf = (unsigned int) strlen(B); \
  281.     if (szf > (unsigned int) (N)) szf = (unsigned int) (N); \
  282.     if (sizeof(A) != sizeof(char*)) { \
  283.       assertf(sz + szf + 1 < sizeof(A)); \
  284.       if (szf > 0) { \
  285.         if (sz + szf + 1 < sizeof(A)) { \
  286.           memcpy((A) + sz, (B), szf); \
  287.           * ( (A) + sz + szf) = '\0'; \
  288.         } \
  289.       } \
  290.     } else if (szf > 0) { \
  291.       memcpybuff((A) + sz, (B), szf); \
  292.       * ( (A) + sz + szf) = '\0'; \
  293.     } \
  294.   } \
  295. } while(0)
  296. #define strcpybuff(A, B) do { \
  297.   assertf( (A) != NULL ); \
  298.   if ( ! (B) ) { assertf( 0 ); } \
  299.   if (htsMemoryFastXfr) { \
  300.     if (sizeof(A) != sizeof(char*)) { \
  301.       (A)[sizeof(A) - 1] = '\0'; \
  302.     } \
  303.     strcpy(A, B); \
  304.     if (sizeof(A) != sizeof(char*)) { \
  305.       assertf((A)[sizeof(A) - 1] == '\0'); \
  306.     } \
  307.   } else { \
  308.     unsigned int szf = (unsigned int) strlen(B); \
  309.     if (sizeof(A) != sizeof(char*)) { \
  310.       assertf(szf + 1 < sizeof(A)); \
  311.       if (szf > 0) { \
  312.         if (szf + 1 < sizeof(A)) { \
  313.           memcpy((A), (B), szf + 1); \
  314.         } else { \
  315.           * (A) = '\0'; \
  316.         } \
  317.       } else { \
  318.         * (A) = '\0'; \
  319.       } \
  320.     } else { \
  321.       memcpybuff((A), (B), szf + 1); \
  322.     } \
  323.   } \
  324. } while(0)
  325.  
  326. #else
  327.  
  328. #ifdef STRDEBUGFAST
  329.  
  330. /* protected strcat, strncat and strcpy - definitely useful */
  331. #define strcatbuff(A, B) do { \
  332.   assertf( (A) != NULL ); \
  333.   if ( ! (B) ) { assertf( 0 ); } \
  334.   if (sizeof(A) != sizeof(char*)) { \
  335.     (A)[sizeof(A) - 1] = '\0'; \
  336.   } \
  337.   strcat(A, B); \
  338.   if (sizeof(A) != sizeof(char*)) { \
  339.     assertf((A)[sizeof(A) - 1] == '\0'); \
  340.   } \
  341. } while(0)
  342. #define strncatbuff(A, B, N) do { \
  343.   assertf( (A) != NULL ); \
  344.   if ( ! (B) ) { assertf( 0 ); } \
  345.   if (sizeof(A) != sizeof(char*)) { \
  346.     (A)[sizeof(A) - 1] = '\0'; \
  347.   } \
  348.   strncat(A, B, N); \
  349.   if (sizeof(A) != sizeof(char*)) { \
  350.     assertf((A)[sizeof(A) - 1] == '\0'); \
  351.   } \
  352. } while(0)
  353. #define strcpybuff(A, B) do { \
  354.   assertf( (A) != NULL ); \
  355.   if ( ! (B) ) { assertf( 0 ); } \
  356.   if (sizeof(A) != sizeof(char*)) { \
  357.     (A)[sizeof(A) - 1] = '\0'; \
  358.   } \
  359.   strcpy(A, B); \
  360.   if (sizeof(A) != sizeof(char*)) { \
  361.     assertf((A)[sizeof(A) - 1] == '\0'); \
  362.   } \
  363. } while(0)
  364.  
  365. #else
  366.  
  367. #define strcatbuff strcat
  368. #define strncatbuff strncat
  369. #define strcpybuff strcpy
  370.  
  371. #endif
  372.  
  373. #endif
  374.  
  375. #endif
  376.  
  377. #ifdef __cplusplus
  378. }
  379. #endif
  380.  
  381. #endif
  382.